package com.example.transaction.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.TimeZone;
import java.util.concurrent.ThreadLocalRandom;

@Configuration
@EnableCaching
public class MyRedisConfig {
    @Resource
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        ObjectMapper objectMapper = JacksonConfig.getObjectMapper();
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper);

        // 使用String序列化器来序列化和反序列化Redis的key
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // 使用GenericJackson2JsonRedisSerializer来序列化和反序列化Redis的value
        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

//    @Bean
//    public RedisTemplate redisTemplate(){
//        RedisTemplate<String,Object>redisTemplate=new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//
//        Jackson2JsonRedisSerializer serializer=new Jackson2JsonRedisSerializer<Object>(Object.class);
//        redisTemplate.setValueSerializer(serializer);
//
//        ObjectMapper om = new ObjectMapper ();
//        om.registerModule(new JavaTimeModule()); // 注册 Java 8 时间模块
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility .ANY);
//        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        om. setTimeZone (TimeZone.getDefault ());
//        om. configure (MapperFeature.USE_ANNOTATIONS, false);
//        om. configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        om.configure (SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance
//                ,ObjectMapper. DefaultTyping.NON_FINAL, JsonTypeInfo. As.PROPERTY);
//        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//        serializer.setObjectMapper(om);
//
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }

}
